# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# This source file is part of the Cangjie project, licensed under Apache-2.0
# with Runtime Library Exception.
#
# See https://cangjie-lang.cn/pages/LICENSE for license information.


from os import path

integer_types = ['Int8', 'Int16', 'Int32', 'Int64', 'IntNative', 'UInt8', 'UInt16', 'UInt32', 'UInt64', 'UIntNative']
number_types = integer_types + ['Float16', 'Float32', 'Float64']
other_types = ['String', 'Rune', 'Bool', 'Unit', '(Int8, Int8)', 'Array<Int8>', 'C']
types = number_types + other_types
default_value_map = {'Bool' : 'true', 'Unit' : '()', 'Array<Int8>' : '[1]', 'Rune' : "'1'", 'String' : '"1"', '(Int8, Int8)' : '(1i8, 1i8)', 'C' : 'C(1)'}
ops = ['', '+', '-', '*', '/', '%', '&&', '||', '&', '|', '^', '<<', '>>']

def default_value(ty : str) -> str:
  if ty in integer_types: return '1'
  if 'Float' in ty: return '1.0'
  return default_value_map[ty]

def valid_types(op: str) -> list:
  if op in ['%', '<<', '>>', '&', '|', '^']: return integer_types
  if op in ['&&', '||']: return ['Bool']
  if not op: return types
  return number_types

dir = path.dirname(path.realpath(__file__))
path = dir + '/test_' + path.basename(dir) + '_{}.cj'
template = '''
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * This source file is part of the Cangjie project, licensed under Apache-2.0
 * with Runtime Library Exception.
 *
 * See https://cangjie-lang.cn/pages/LICENSE for license information.
 */

/*
  @Assertion:   4.23(16) The type and the value of an assignment expression is Unit and (), respectively.
  @Description: %s
  @Mode: %s
  @Negative: %s
  @Structure: single
  @CompileWarning: ignore
  @Comment: Auto-generated by gen.py
*/
%s'''
positive_template = template % ('Checks that the type and the value of assignment expressions is Unit and (), respectively.',
                                'run', 'no', '''
from utils import utils.assert.Assert
%s
%s

main() {
%s
%s
    return 0
}
''')
class_decl = '''
class C {
    let x : Int64
    C(y : Int64) {
        x = y
    }
}
'''
counter = 1

def write_counted(contents : str):
  global counter
  with open(path.format(str(counter).zfill(3)), 'w') as file:
    file.write(contents)
    counter += 1

def mangle(t : str):
  if t == '(Int8, Int8)': return 'Tuple'
  if 'Array' in t: return 'Array'
  return t

write_counted(positive_template % (class_decl,
    '\n'.join(['var v%s : %s = %s' % (mangle(t), t, default_value(t)) for t in types]),
    '\n'.join(['    Assert.isTrue(({lhs} {op}= {rhs}) is Unit)'.format(
               lhs='v' + mangle(t), op=op, rhs=default_value(t)) for op in ops for t in valid_types(op)]),
    '\n'.join(['    Assert.isTrue(({lhs} {op}= {rhs}) == ())'.format(
               lhs='v' + mangle(t), op=op, rhs=default_value(t)) for op in ops for t in valid_types(op)])))
